using NL.UI;
using NL;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace XGame.UI
{
    public class WinLevelUp : Window
    {
    	private string Level;
        public Text level_txt;
        public Text countdown_txt;
    	private int BattleType; // 0:normal  1:multiplay

        public List<UISkillItem> skill_items;

        private IniArrAction _ONSkillsQuery;

        private int first_skill_id;

        private float muti_wait_time;

        public WinLevelUp WaitSkillsQuery(IniArrAction _on)
        {
            _ONSkillsQuery = _on;
            return this;
        }

        public WinLevelUp SetLevel(int _level)
        {
            Level = _level+"";
            return this;
        }

        public WinLevelUp SetBattleType(int _btype)
        {
            BattleType = _btype;
            return this;
        }

        public WinLevelUp SetMutiWaitTime(float _time)
        {
            muti_wait_time = _time;
            return this;
        }

        protected override void OnShow()
        {
            base.OnShow();

			level_txt.text = Level;

			var arr = _ONSkillsQuery?.Invoke();

            List<NLIni> _infos = new List<NLIni>();
            _infos.AddRange(arr);

			for(int i = 0; i < _infos.Count; i++)
            {
                first_skill_id = _infos[0]["id"];
                skill_items[i].gameObject.INFOSet(_infos[i]);
                skill_items[i].WaitClick((_id)=>{Close();time_count=0;return _LevelUpSelectSkill?.Invoke(_id);}).Show();
            }

            countdown_txt.gameObject.SetActive(false);

            if (BattleType == 1)// 多人
            {
                countdown_txt.gameObject.SetActive(true);
                time_count = muti_wait_time;
            }

        }

        private float time_count = 0;
        private void Update()
        {
            if (time_count > 0)
            {
                time_count -= Time.deltaTime;

                if (time_count <= 0)
                {
                    time_count = 0;
                    // 选个技能
                    _LevelUpSelectSkill?.Invoke(first_skill_id);
                    Close();
                }
                countdown_txt.text = "RANDOM SELECTION IN:" + Mathf.CeilToInt(time_count) + "S";
            }

        }

        IniAction<int> _LevelUpSelectSkill;
        public WinLevelUp WaitSelectSkill(IniAction<int> _on)
        {
            _LevelUpSelectSkill = _on;
            return this;
        }

    }
}

